// ---------------------------------------------------
// Author:     code34 nicolas_boiteux@yahoo.fr
// Edited by:  Neumatic
// Warcontext: Detacte Ied and playSound when nearest
// ---------------------------------------------------

private ["_printed","_counter","_sound","_objects"];

_printed = false;

while {true} do {
	if ((wceverybodyengineer == 1) or (typeOf player in wcengineerclass)) then {
		_objects = nearestObjects [player, ["All"], 30];
		{
			if (_x getVariable "wciedactivate") then {
				if !(_printed) then {
					wcgarbage = [localize "STR_WC_MESSAGEDETECTORIED", localize "STR_WC_MESSAGETRYTOMOVEAROUND", localize "STR_WC_MESSAGEIEDHASBEENDETECT", 10] spawn WC_fnc_playerhint;
					_printed = true;
					_counter = 0;
				};
				if (vehicle player == player) then {
					if (player distance _x > 20) then {
						if (random 1 < 0.8) then {
							playSound "bombdetector2";
						} else {
							_sound = ["bombdetector1", "bombdetector2", "bombdetector3"] call BIS_fnc_selectRandom;
							playSound _sound;
						};
					} else {
						if (player distance _x > 10) then {
							if (random 1 < 0.9) then {
								playSound "bombdetector3";
							} else {
								_sound = ["bombdetector1", "bombdetector2", "bombdetector3"] call BIS_fnc_selectRandom;
								playSound _sound;
							};
						} else {
							if (random 1 < 0.97) then {
								playSound "bombdetector1";
							} else {
								_sound = ["bombdetector1", "bombdetector2", "bombdetector3"] call BIS_fnc_selectRandom;
								playSound _sound;
							};
						};
					};
				};
			};
			sleep 0.01;
		} foreach _objects;

		// False - positive
		if ((random 1 > 0.995) and wciedfalsepositive) then {
			if (player distance getMarkerPos "respawn_west" > 1000) then {
				if ((getPosATL player) select 2 < 1) then {
					wcgarbage = [localize "STR_WC_MESSAGEDETECTORIED", localize "STR_WC_MESSAGETRYTOMOVEAROUND", localize "STR_WC_MESSAGEIEDHASBEENDETECT", 10] spawn WC_fnc_playerhint;
					_printed = true;
					_counter = 0;
					_sound = ["bombdetector1", "bombdetector2", "bombdetector3"] call BIS_fnc_selectRandom;
					playSound _sound;
				};
			};
		};

		if (_printed) then {
			_counter = _counter + 1;
			if (_counter > 15) then {
				_printed = false;
			};
		};
	};

	sleep 1;
};